﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockImport.dataimport.common;
using StockImport.http;
using StockCommon.Calendar;
using Utilities;
using StockCommon.TaskLog;
using System.IO;
using NetLib.web;

namespace StockImport.dataimport.otc.dailyindex
{
    public class OTC_DailyIndexFetcher : HttpContentFetcher_Daily
    {
        protected Guid taskId;
        public OTC_DailyIndexFetcher()
        {
            taskId = ObjectExecServices.getServicesId("sitefetcher", getTaskName(), getVersion());
        }


        public override DateTime getStartDate()
        {
            return DateTime.Parse("2012/05/29");
        }

        public override DateTime getEndDate()
        {
            return DateTime.Parse("2014/01/01");
        }

        public override string getVersion()
        {
            return "2.0";
        }

        public override string getTaskName()
        {
            return "上櫃_股票行情";
        }

        protected String getURL2(DateTime date)
        {
            String pat = "http://www.gretai.org.tw/ch/stock/aftertrading/otc_quotes_no1430/squote_AL_${yy}${MM}${dd}.html";
            return replaceYMD(pat, date);
        }
        public override string getURL(DateTime date)
        {
            String pat = "http://www.gretai.org.tw/ch/stock/aftertrading/otc_quotes_no1430/squote_EW_${yy}${MM}${dd}.html";
            return replaceYMD(pat, date);
        }

        public override ProcessResultEnum Update(DateTime date, bool force)
        {
            date=DateUtils.getDateOnly(date);
            UpdateResult updateResult = new UpdateResult();
            updateResult.操作 = "上櫃股票指數抓取";
            updateResult.資料日期 = date;

            
            if (CalenderHelper.isWorkDay(date) == false)
                return ProcessResultEnum.完成且不需更新;
            
            DateTime dt = DateUtils.getTimeAtDay(date, 14);
            if (DateTime.Now.CompareTo(dt) < 0)
                return ProcessResultEnum.必須重做;

            //取的ObjectExecLog
            StockCommon.ObjectExecLog log = ObjectExecServices.getExecLog(taskId, "get", date);
            if (force == false)
                if (islogComplete(log))
                    return ProcessResultEnum.完成且不需更新;
            log.lastExecuteStart = DateTime.Now;
            //----------------------------------------------------
            //Remove. Data.
            //----------------------------------------------------
            String fileName = getStoreFileName(date);

            String url = getURL(date);

            bool success = false;
            String opMessage = "";
            String content = HttpGet.process(url, out success, out opMessage);
            if (success == false)
            {
                content = HttpGet.process(getURL2(date), out success, out opMessage);
            }

            log.lastExecuteEnd = DateTime.Now;

            ProcessResultEnum result;
            if (success)
            {
                log.status = (int)LogStatus.sUpdated;
                if (String.IsNullOrWhiteSpace(content) == false)
                {
                    FileUtils.saveUTF8(fileName, content);
                    log.result = (int)LogResult.rComplete;
                    result = ProcessResultEnum.完成且有資料;
                    log.msg = "資料長度:" + (content.Length/1000).ToString()+"KB";
                }
                else
                {
                    log.result = (int)LogResult.rNoData;
                    log.msg = "無資料";
                    result = ProcessResultEnum.必須重做;
                }
                
            }
            else
            {
                log.status = (int)LogStatus.sConnectFailed;
                log.msg = opMessage;
                result = ProcessResultEnum.發生錯誤;
            }
            ObjectExecServices.updateExecLog(log);
            return result;
            
        }
        private static String getStorePath()
        {
            return @"D:\stockdata\otc\上櫃_股票行情\";
        }
        public static string getStoreFileName(DateTime date)
        {
            String sp = getStorePath();
            sp += String.Format("original\\{0}.html", DateUtils.toShortDateFormat_NoSlash(date));
            return sp;
        }
    }
}
